﻿using FbHelperWP7NonUi.TextHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Text;
using System.Collections;

namespace FbHelperNonUiTests
{
    [TestClass()]
    public class TextBookTest
    {
        private const string bookEntry =
@"My book
sfgdfg
dfg
dfg
dfgdfgdfgdfg

dfgdfgdfg



dfgdfgdfgdfgdfgdfgdfg";

        [TestMethod()]
        public void OpenEmptyTest()
        {
            using (Stream stream = GetFromString( String.Empty ))
            {
                TextBook book = TextBook.Open( stream );

                Assert.IsNotNull( book );
                Assert.IsNotNull( book.Authors );
                Assert.IsNotNull( book.Title );
                Assert.IsTrue( String.IsNullOrWhiteSpace( book.Title ) );
                Assert.IsNotNull( book.Text );
                Assert.AreEqual( 1, book.Text.Count );
            }
        }

        [TestMethod()]
        public void OpenTest()
        {
            using (Stream stream = GetFromString( bookEntry ))
            {
                TextBook book = TextBook.Open( stream );

                Assert.IsNotNull( book );
                Assert.IsNotNull( book.Authors );
                Assert.AreEqual( "My book", book.Title );

                Assert.IsNotNull( book.Text );
                CollectionAssert.AllItemsAreInstancesOfType( book.Text, typeof( SimpleText ) );

                foreach (SimpleText str in book.Text)
                {
                    Assert.IsFalse( str.Text.Contains( "\n" ) );
                }
            }
        }

        private static MemoryStream GetFromString( string input )
        {
            return new MemoryStream( Encoding.UTF8.GetBytes( input ) );
        }
    }
}
